// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({env: 'test-qch4n'})
const db = cloud.database({env: 'test-qch4n'})
// 云函数入口函数
exports.main = async (event, context) => {
  let { page = 1, pages = 20 } = event
  let { OPENID } = cloud.getWXContext()
  const { total = 0 } = await db.collection('warnList').where({
    _openid: OPENID
  }).count()
  const batchTimes = Math.ceil(total / pages)
  if (page <= batchTimes) {
    try {
      let res = await db.collection('warnList')
      .where({_openid: OPENID})
      .skip((page - 1) * pages)
      .limit(pages)
      .get()
      return {
        code: 0,
        data: res,
        page,
        pages,
        total
      }
    } catch (err) {
      console.log(err)
    }
  } else {
    return {
      code: 1
    }
  }
}